package com.qiuzhiyi.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.li.widget.CircleImageView;
import com.qiuzhiyi.R;
import com.qiuzhiyi.activity.AgentActivity;
import com.qiuzhiyi.activity.AllowanceRecommendActivity;
import com.qiuzhiyi.activity.LoginActivity;
import com.qiuzhiyi.activity.MyFriendsActivity;
import com.qiuzhiyi.activity.NoteActivity;
import com.qiuzhiyi.activity.PersonalInfoActivity;
import com.qiuzhiyi.activity.SettingActivity;
import com.qiuzhiyi.activity.ShopActivity;
import com.qiuzhiyi.base.QZYBaseFragment;
import com.qiuzhiyi.cache.HumanCache;
import com.qiuzhiyi.item.HumanItem;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: Li
 * Date: 2017/11/13
 * Description: "我的"
 */

public class MineFragment extends QZYBaseFragment {
    @BindView(R.id.img_avatar)
    CircleImageView mImgAvatar;
    @BindView(R.id.txt_name)
    TextView mTxtName;
    @BindView(R.id.txt_years)
    TextView mTxtYears;

    private SparseArray<Class> mClassArray;

    public static MineFragment newInstance() {
        Bundle bundle = new Bundle();

        MineFragment fragment = new MineFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        mClassArray = new SparseArray<>();
        mClassArray.put(R.id.txt_edit, PersonalInfoActivity.class);
        mClassArray.put(R.id.img_avatar, PersonalInfoActivity.class);
        mClassArray.put(R.id.txt_name, PersonalInfoActivity.class);
        mClassArray.put(R.id.txt_years, PersonalInfoActivity.class);

        mClassArray.put(R.id.my_friend, MyFriendsActivity.class);
        mClassArray.put(R.id.my_allowance, AllowanceRecommendActivity.class);
        mClassArray.put(R.id.my_recommend, AllowanceRecommendActivity.class);
        mClassArray.put(R.id.my_shop, ShopActivity.class);

        mClassArray.put(R.id.my_agent, AgentActivity.class);
//        mClassArray.put(R.id.my_question, MyQuestionActivity.class);
        mClassArray.put(R.id.my_note, NoteActivity.class);
        mClassArray.put(R.id.setting, SettingActivity.class);

    }

    @OnClick({R.id.txt_edit, R.id.img_avatar, R.id.txt_name, R.id.txt_years,
            R.id.my_friend, R.id.my_allowance, R.id.my_recommend, R.id.my_shop,
            R.id.my_agent, R.id.my_note, R.id.setting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.my_allowance:
                Bundle bundle = new Bundle();
                bundle.putString("type", "0");
                startWithArg(mClassArray.get(view.getId()), bundle);
                break;
            case R.id.my_recommend:
                Bundle b = new Bundle();
                b.putString("type", "1");
                startWithArg(mClassArray.get(view.getId()), b);
                break;
            case R.id.my_shop:
            case R.id.my_agent:
                start(mClassArray.get(view.getId()));
                break;
            default:
                mItem = HumanCache.getHumanInfo(mContext);
                if (mItem == null) {
                    mUid="";
                }else {
                    mUid=String.valueOf(mItem.getHumanId());
                }
                if (TextUtils.isEmpty(mUid)) {
                    start(LoginActivity.class);
                    return;
                }
                start(mClassArray.get(view.getId()));

                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        HumanItem item = HumanCache.getHumanInfo(mContext);
        if (item != null) {
            mTxtYears.setVisibility(View.VISIBLE);
            Glide.with(mContext)
                    .load(item.getHumanPicUrl())
                    .error(R.mipmap.icon)
                    .into(mImgAvatar);
            mTxtName.setText(item.getHumanQuestion().equals("") ? item.getHumanAccount() : item.getHumanQuestion());
            String year = item.getHumanAnswer().substring(0, 4);
            int currentYear = Calendar.getInstance().get(Calendar.YEAR);
            int years = currentYear - Integer.parseInt(year);
            String y = String.valueOf(years < 0 ? 0 : years) + "岁";
            mTxtYears.setText(y);
            String sex = item.getHumanSex();
            mTxtYears.setCompoundDrawablesWithIntrinsicBounds(
                    sex.equals("1") ? R.drawable.nanxing : R.drawable.nvxing,
                    0, 0, 0);
        } else {
            mTxtName.setText("未登录");
            mTxtYears.setVisibility(View.INVISIBLE);
            Glide.with(mContext).load(R.mipmap.icon)
                    .into(mImgAvatar);
        }
    }
}
